package com.xiaoshi.hander;

import com.xiaoshi.enums.JsonReturnEnums;
import com.xiaoshi.utils.JsonResult;
import com.xiaoshi.utils.ResUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 处理资源服务器异常  403    token无效
 */
public class ResAuthExceptionEntryPoint implements AuthenticationEntryPoint {


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {

        if(StringUtils.containsIgnoreCase(authException.getMessage(), "Full authentication is required to access this resource")){
            ResUtil.makeJsonResult(
                    response, MediaType.APPLICATION_JSON_UTF8_VALUE,
                    JsonResult.fail(JsonReturnEnums.AUTH_IS_NULL)
            );

        }else {
            ResUtil.makeJsonResult(
                    response, MediaType.APPLICATION_JSON_UTF8_VALUE,
                    JsonResult.fail(JsonReturnEnums.INVALID_TOKEN)
            );

        }


    }
}